package com.boco.webmaster.model;

import java.util.Map;
import java.util.HashMap;
import com.boco.transnms.server.bo.helper.cm.*;
import com.boco.transnms.client.model.base.BoCmdFactory;
import com.boco.transnms.client.model.base.IBoCommand;
import com.boco.transnms.common.dto.base.BoActionContext;
import com.boco.transnms.common.dto.base.DataObjectList;
import com.boco.transnms.common.dto.Site;
import java.util.Iterator;

public class SiteModel {
    private static SiteModel instance = new SiteModel();
    private Map siteMap = new HashMap();
    public SiteModel() {
        refresh();
    }

    public void refresh(){
        IBoCommand cmd = BoCmdFactory.getInstance().createBoCmd(SiteBOHelper.ActionName.getAllSite, new Object[] {new BoActionContext()});
        DataObjectList dbos = (DataObjectList) cmd.exec();
        for (int i = 0; i < dbos.size(); i++) {
            Site dbo = (Site)dbos.get(i);
            siteMap.put(dbo.getCuid(),dbo);
        }
    }

    public static SiteModel getInstance() {
        return instance;
    }

    public Site getSiteByCuid(String cuid){
        return (Site)siteMap.get(cuid);
    }

    public String getCuidByLabelCn(String label_cn){
        Iterator i = siteMap.keySet().iterator();
        while (i.hasNext()) {
            String site_cuid = (String) i.next();
            Site dbo = (Site) siteMap.get(site_cuid);
            if(dbo.getLabelCn().equals(label_cn)){
                return dbo.getCuid();
            }
        }
        return null;
    }

    public String getNameByCuid(String cuid){
        Site dbo = (Site) siteMap.get(cuid);
        if (dbo != null) {
            return dbo.getLabelCn();
        } else {
            return "";
        }
   }


    public DataObjectList getAllSite(){
        DataObjectList dbos = new DataObjectList();
        Iterator i = siteMap.keySet().iterator();
        while (i.hasNext()) {
            String site_cuid = (String) i.next();
            Site dbo = (Site) siteMap.get(site_cuid);
            dbos.add(dbo);
        }
        return dbos;
    }

    public DataObjectList getSiteByDistrictCuid(String districtCuid){
        DataObjectList dbos = new DataObjectList();
        Iterator i = siteMap.keySet().iterator();
        while (i.hasNext()) {
            String site_cuid = (String) i.next();
            Site dbo = (Site) siteMap.get(site_cuid);
            if(dbo.getRelatedSpaceCuid().equals(districtCuid)){
                dbos.add(dbo);
            }
        }
        return dbos;
    }

    public void addSite(Site dbo){
        siteMap.put(dbo.getCuid(),dbo);
    }

    public void delSite(Site dbo){
        siteMap.remove(dbo.getCuid());
    }

    public void modifySite(Site dbo){
        siteMap.remove(dbo.getCuid());
        siteMap.put(dbo.getCuid(),dbo);
    }

    public Site getSiteByLabelCn(String label_cn){
        Iterator i = siteMap.keySet().iterator();
        while (i.hasNext()) {
            String site_cuid = (String) i.next();
            Site dbo = (Site) siteMap.get(site_cuid);
            if(dbo.getLabelCn().equals(label_cn)){
                return dbo;
            }
        }
        return null;
    }
}
